package com.fh.service.impl;

import com.fh.config.OrderaddClient;
import com.fh.mapper.OrderAddMapper;
import com.fh.model.Orders;
import com.fh.model.vo.ShoppingCartVO;
import com.fh.service.OrderAddService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class OrderAddServiceImpl implements OrderAddService {

    @Resource
    private OrderaddClient orderaddClient;
    @Resource
    private OrderAddMapper orderAddMapper;

    @Override
    public List<ShoppingCartVO> addOrder( String cids,Orders orders) {
        String[] arr = cids.split(",");
        List<Integer> list=new ArrayList<>();
        for (String cid : arr) {
            list.add(Integer.parseInt(cid));
        }
        List<ShoppingCartVO> shoppingCartVOS = orderaddClient.queryStock(list);
        boolean stock=true;
        String  untitled="";
        for (ShoppingCartVO cartVO : shoppingCartVOS) {
            if (Integer.parseInt(cartVO.getCartNum())>cartVO.getStock()){
                stock=false;
                break;
            }
            untitled=untitled+cartVO.getProductName()+",";
        }
        if (stock) {
            //保存订单
            orders.setUntitled(untitled.substring(0, untitled.length() - 1));
            orders.setCreateTime(new Date());
            orders.setStatus("1");
            String orderId = UUID.randomUUID().toString().replace("-", "");
            orders.setOrderId(orderId);
            int i = orderAddMapper.insert(orders);
            if (i>0){
                return  shoppingCartVOS;
            }

        }
        return null;
    }
}
